package com.common.controller;

import com.common.annotation.LogAnnotation;
import com.common.controller.base.BaseAjaxController;
import com.common.dto.PrivilegeDTO;
import com.common.service.PrivilegeService;
import com.common.util.ConstantDefine;
import com.common.util.LogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/privilege")
public class PrivilegeContoller extends BaseAjaxController<PrivilegeDTO> {

    @Autowired
    private PrivilegeService privilegeService;

    /**
     * Created by wrl on 2017/07/11
     * 初始化更新页面
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateInit/{id}")
    public PrivilegeDTO selectByKey(@PathVariable("id") String id) {
        if (!"undefined".equals(id)) {
            PrivilegeDTO privilegeDTO = privilegeService.selectByKey(id);
            return privilegeDTO;
        }
        return null;
    }

    @Override
    public PrivilegeDTO updateInit(@PathVariable("id") String id) {
        return null;
    }

    @Override
    public ResponseEntity<Map<String, Object>> update(@Valid PrivilegeDTO entity, BindingResult bindingResult) {
        return null;
    }


    @Override
    public String init(Model model) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String init(PrivilegeDTO entity, Model model) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * Created by wrl on 2017/07/11
     * 增
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping("/save")
    @LogAnnotation(description = "权限单个添加", bizID = LogType.PRIVILEGE)
    public Map<String, Object> saveDTO(PrivilegeDTO entity) {
        Map<String, Object> map = new HashMap<>();
        map.put(ConstantDefine.FLAG, privilegeService.save(entity));
        return map;
    }

    /**
     * Created by wrl on 2017/07/13
     * 修改
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @LogAnnotation(description = "权限单个更新", bizID = LogType.PRIVILEGE)
    public Map<String, Object> updateDTO(PrivilegeDTO entity) {
        Map<String, Object> map = new HashMap<>();
        int count = privilegeService.updateNotNull(entity);
        if (count == 1) {
            map.put(ConstantDefine.FLAG, true);
        }
        return map;
    }

    @Override
    public ResponseEntity<Map<String, Object>> queryByEntity(PrivilegeDTO entity) {
        return null;
    }

    @Override
    public ResponseEntity<Map<String, Object>> save(@Valid PrivilegeDTO entity, BindingResult bindingResult) {
        return null;
    }

    @Override
    public ResponseEntity<Map<String, Object>> delete(List<String> id) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * Created by wrl on 2017/07/13
     * 删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    public Map<String, Object> deleteByIds(String[] ids) {
        List<String> list = new ArrayList<>();
        for (int index = 0; index < ids.length; index++) {
            list.add(ids[index]);
        }
        Map<String, Object> map = privilegeService.deleteByIds(list);
        return map;
    }

    /**
     * Created by wrl on 2017/07/13
     * 删除
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteById")
    public Map<String, Object> deleteById(PrivilegeDTO entity) {
        Map<String, Object> map = new HashMap<>();
        String id = entity.getId();
        if (null != id && !"".equals(id)) {
            int count = privilegeService.deleteByPrimaryKey(id);
            if (count == 1)
                map.put(ConstantDefine.FLAG, true);
            return map;
        }
        return null;
    }

    /**
     * Created by wrl on 2017/07/11
     * 分页查询：排序
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectPaging")
    @LogAnnotation(description = "权限管理页面", bizID = LogType.PRIVILEGE)
    public Map<String, Object> selectByPaging(PrivilegeDTO entity) {
        Map<String, Object> resultMap = privilegeService.selectFunctionPaging(entity);
        return resultMap;
    }

    /**
     * Created by wrl on 2017/07/13
     * 分页查询
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping("/search")
    public Map<String, Object> search(PrivilegeDTO entity) {
        entity.setIsSearch("1");
        Map<String, Object> resultMap = privilegeService.selectFunctionPaging(entity);
        return resultMap;
    }

    /**
     * 获取所有权限-分级
     * Created by wrl on 2017/07/11
     * 分级查询所有记录
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/select")
    public Map<String, Object> selectByGraded(PrivilegeDTO entity) {
        Map<String, Object> map = privilegeService.selectByGraded(entity);
        return map;
    }

    /**
     * 获取前两级权限
     * Created by wrl on 2017/09/14
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/authority2Show")
    public Map<String, Object> selectAuthorityByType(PrivilegeDTO entity) {
        Map<String, Object> map = privilegeService.selectAuthorityByType(entity);
        return map;
    }

    /**
     * 获取前button级权限
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/buttonAuthority")
    public Map<String, Object> selectButtonAuthority(PrivilegeDTO entity) {
        Map<String, Object> map = new HashMap<>();
        List priviList = privilegeService.selectButtonAuthority();
        map.put("btnPrivi", priviList);
        return map;
    }
}
